/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.hibernate.entities;

import com.oea.hibernate.utilities.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Sony
 */
@Table(name = "students")
@Entity
public class Student extends AbstractEntity {

    @Id
    @GeneratedValue
    @Column(name = "id")
    protected Long id;
    @Column(name = "fname")
    protected String fname;
    @Column(name = "mname")
    protected String mname;
    @Column(name = "lname")
    protected String lname;
    @Column(name = "gender")
    protected String gender;
    @Column(name = "email")
    protected String email;
    @Column(name = "password")
    protected String password;
    @Column(name = "enrollmentno")
    protected String enrollmentno;
    @Column(name = "contact")
    protected String contact;

    @Column(name = "divid")

    protected Long divid;

    @Transient
    protected Division division;

    @Column(name = "status")
    protected boolean status;
    @Column(name = "addedby")
    protected Long addedby;

    @Transient
    protected Admin admin;

    public Division getDivision() {
        return division;
    }

    public void setDivision() {
        this.division = (Division) new Division().getById(this.divid);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getMname() {
        return mname;
    }

    public void setMname(String mname) {
        this.mname = mname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEnrollmentno() {
        return enrollmentno;
    }

    public void setEnrollmentno(String enrollmentno) {
        this.enrollmentno = enrollmentno;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public Long getDivid() {
        return divid;
    }

    public void setDivid(Long divid) {
        this.divid = divid;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public Long getAddedby() {
        return addedby;
    }

    public void setAddedby(Long addedby) {
        this.addedby = addedby;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin() {
        this.admin = (Admin) new Admin().getById(addedby);
    }

    public Student verify() {
        Student s = null;
        try {
            Session ss = HibernateUtil.getSessionFactory().openSession();
            Query q = ss.createQuery("from Student where email='" + this.email + "' and password='" + this.password + "' and status=" + true + "");

            List<Student> li = q.list();

            if (li.size() == 1) {
                s = li.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return s;
    }

//    public static List<Student> getallstudents() {
//        List<Student> li = null;
//        try {
//            Session s = HibernateUtil.getSessionFactory().openSession();
//
//            Query q = s.createQuery("from Student");
//
//            li = q.list();
//
//        } catch (Exception e) {
//        }
//        return li;
//    }
    public boolean studentupdate() {
        boolean b = false;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = s.beginTransaction();
            //  System.out.println("parth");
            Query q = s.createQuery("update Student set status=" + this.status + "  where id=" + this.id + "");
            //      System.out.println("arpan");
//            q.setParameter("statuss", Boolean.TRUE);
//            q.setParameter("id", stud_id);
            q.executeUpdate();
         //   System.out.println("keval");

            //   System.out.println("bjghjbjhb");
            tx.commit();
            b = true;

        } catch (Exception e) {
        }
        return b;
    }

    public static ArrayList<Student> pageStudent(List<Student> li) {
        ArrayList<Student> li_st = new ArrayList<Student>();

        //  ArrayList<Student> filtered_list = new ArrayList<>();
        int end = 5;
        if (li.size() < 5) {
            end = li.size();
        }
        for (int i = 0; i < end; i++) {
            System.out.println(li.get(0));
            //  li_st.add(li.get(i));
            li_st.add(li.get(i));
        }

        return li_st;
    }

    public static List<Student> GetStudentByDivId(Long divid) {
        List<Student> li_student = null;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Query q = s.createQuery("from Student where  divid=" + divid + "");
            li_student = q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return li_student;
    }

    public static List<Student> GetStudentByBranchAndSemesterId(Long branchid, Long semester) {
        List<Student> li_student_by_branchid_divisionid = null;

        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Query q = s.createQuery("from Student where divid in  (select id from Division where bid=" + branchid + " AND semester=" + semester + ")");
            li_student_by_branchid_divisionid = q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return li_student_by_branchid_divisionid;
    }

    public static List<Student> GetStudentByBranchId(Long branchid) {
        List<Student> li_student_by_branchid = null;

        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Query q = s.createQuery("from Student where divid in (select id from Division where bid=" + branchid + ")");
            li_student_by_branchid = q.list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return li_student_by_branchid;
    }

    public List<Exam> getMyExams() {
        List<Exam> myexams = null;

        this.setDivision();

        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Query q = s.createQuery("from Exam where bid=" + this.getDivision().getBid());
            myexams = q.list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return myexams;
    }
    
    public static List<Student> getAllStudentsByDivid(Long div_id) {
        List<Student> li = null;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Query q = s.createQuery("from Student where divid= " + div_id);
            li = q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return li;
    }

}
